package com.statistics.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.statistics.bean.Electricity;
import com.statistics.bean.ElectricityWeek;
import com.statistics.bean.MaxCurrentPower;
import com.statistics.cloudpower.dao.TblElectricityStatisticsDayDAO;
import com.statistics.cloudpower.dao.TblElectricityStatisticsWeekDAO;
import com.statistics.cloudpower.entity.TblElectricityStatisticsWeek;
import com.statistics.cloudpower.entity.TblEquipment;
import com.statistics.tools.Constants;
import com.statistics.tools.Tools;
import com.statistics.yfdata.dao.DataElectricityRepository;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author MәӧωρaЯsε
 * @date 2017/7/24.
 */
@Component
@Scope("prototype")
public class ElectricityStatisticsWeekThread extends Thread {

    private Logger logger;
    private String uuid;
    private List<TblEquipment> obj;
    private int fragment_number;
    private TblElectricityStatisticsWeekDAO tblElectricityStatisticsWeekDAO;
    private DataElectricityRepository dataElectricityRepository;

    public ElectricityStatisticsWeekThread(List<TblEquipment> obj,
                                           TblElectricityStatisticsWeekDAO dao, String uuid, int fragment_number,
                                           DataElectricityRepository dataElectricityRepository) {

        this.obj = obj;
        this.fragment_number = fragment_number;
        this.uuid = uuid;
        this.dataElectricityRepository = dataElectricityRepository;
        this.tblElectricityStatisticsWeekDAO = dao;
        this.logger = Logger.getLogger("电量周统计");
    }

    @Override
    public void run() {

        //根据读取的command进行相应运算
        try {
            DateFormat dataFormat = DateFormat.getDateInstance();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String thread_start_time = simpleDateFormat2.format(new Date());

            String now = dataFormat.format(new Date());
            Date today = simpleDateFormat.parse(now);
            Long start_date;//前一天起始时间的时间戳
            Long end_date;//前一天结束时间的时间戳

            List<TblEquipment> tblEquipment = obj;
            List records = null;
            int equipment_number;

            //每周电量统计
            start_date = today.getTime() - 24 * 3600 * 1000 * 7;
            end_date = today.getTime();

            logger.info(start_date);
            logger.info(end_date);

            records = new ArrayList<TblElectricityStatisticsWeek>();

            if (records == null) {
                return;
            }

            //对这n条设备数据逐条进行查找相应的电量列表
            for (equipment_number = 0; equipment_number < tblEquipment.size(); equipment_number++) {
                JSONArray jsonArray = new JSONArray();

                if (tblEquipment.get(equipment_number) == null) {
                    continue;
                }

                //取得头尾数据差值
                String mac = tblEquipment.get(equipment_number).getMac();

                List<ElectricityWeek> electricity = dataElectricityRepository.getSubtractionByWeek(mac, start_date, end_date);

                if (electricity == null) {
                    //没有这条设备记录
                    continue;
                }

                //获取8个房间各自的当天用电量
                for (ElectricityWeek electricityWeek : electricity) {

                    Double d_value = Double.parseDouble(electricityWeek.getTotalElectricity());
                    BigDecimal total_electricity = new BigDecimal((d_value) * 0.01);

                    Double d_maxCurrent = Double.parseDouble(electricityWeek.getMaxCurrent());
                    BigDecimal max_current = new BigDecimal((d_maxCurrent) * 0.01);

                    Double d_maxPower = Double.parseDouble(electricityWeek.getMaxPower());
                    BigDecimal max_power = new BigDecimal((d_maxPower) * 0.01);

                    TblElectricityStatisticsWeek one_record = new TblElectricityStatisticsWeek();
                    one_record.setMac(mac);
                    one_record.setOffset(electricityWeek.getOffset());
                    one_record.setTotalElectricity(total_electricity);
                    one_record.setStatisticsTime(new Timestamp(start_date));
                    one_record.setMax_current(max_current);
                    one_record.setMax_power(max_power);

                    records.add(one_record);

                    JSONObject jsonObject = Tools.getJsonEleStr(mac,electricityWeek.getOffset(),total_electricity,max_current,max_power,start_date);
                    jsonArray.add(jsonObject);
                }
//                Tools.sendTo61(Constants.DIANBIAOWEEK,jsonArray.toJSONString());
            }

            tblElectricityStatisticsWeekDAO.save(records);

            records.clear();

            String thread_end_time = simpleDateFormat2.format(new Date());
            logger.info(uuid + " 第" + fragment_number + "分片 Count=" + equipment_number + " Start=" + thread_start_time + " End=" + thread_end_time);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }



}
